//
//  AddDisposeAlarmCell.m
//  efairy
//
//  Created by 李锋 on 2019/3/1.
//  Copyright © 2019年 East. All rights reserved.
//

#import "AddDisposeAlarmCell.h"

@interface AddDisposeAlarmCell ()<UITextViewDelegate>

@end

@implementation AddDisposeAlarmCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}

-(void)initView{
    _alarmTypeView = [[UIView alloc]init];//报警类型
    _alarmTypeView.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *alarmTypeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickAlarmType)];
    [_alarmTypeView addGestureRecognizer:alarmTypeTap];
    
    
    _alarmTypeTitle = [[UILabel alloc]init];//报警类型
    _alarmTypeTitle.text = @"报警类型";
    _alarmTypeTitle.textColor = ColorFromRGB(0x333333);
    _alarmTypeTitle.font = [UIFont systemFontOfSize:15.f];
    _alarmTypeValue= [[UILabel alloc]init];//报警类型
    _alarmTypeValue.textColor = ColorFromRGB(0x999999);
    _alarmTypeValue.font = [UIFont systemFontOfSize:15.f];
    _alarmTypeValue.text = @"请选择";
    _alarmTypeValue.textAlignment = NSTextAlignmentRight;
    UIImageView *rightArrow = [[UIImageView alloc]init];
    rightArrow.image = [UIImage imageNamed:@"icon_right_arrow"];
    [_alarmTypeView sd_addSubviews:@[_alarmTypeTitle,_alarmTypeValue,rightArrow]];
    _alarmTypeTitle.sd_layout.widthIs(122).heightIs(15).leftSpaceToView(_alarmTypeView, 12)
    .topSpaceToView(_alarmTypeView, 14.5);
    _alarmTypeValue.sd_layout.widthIs(92).heightIs(15).rightSpaceToView(_alarmTypeView, 28)
    .topSpaceToView(_alarmTypeView, 14.5);
    rightArrow.sd_layout.widthIs(6.9).heightIs(11.4).rightSpaceToView(_alarmTypeView, 12)
    .topSpaceToView(_alarmTypeView, 16.3);
    
    
    _hiddenDangerView = [[UIView alloc]init];//隐患是否排除
     _hiddenDangerView.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *hiddenDangerTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickHiddenDanger)];
    [_hiddenDangerView addGestureRecognizer:hiddenDangerTap];
    
    _hiddenDangerTitle= [[UILabel alloc]init];
    _hiddenDangerTitle.text = @"隐患是否排除";
    _hiddenDangerTitle.textColor = ColorFromRGB(0x333333);
    _hiddenDangerTitle.font = [UIFont systemFontOfSize:15.f];
    _hiddenDangerValue= [[UILabel alloc]init];
    _hiddenDangerValue.textColor = ColorFromRGB(0x999999);
    _hiddenDangerValue.font = [UIFont systemFontOfSize:15.f];
    _hiddenDangerValue.text = @"否";
    _hiddenDangerValue.textAlignment = NSTextAlignmentRight;
    UIImageView *rightArrow2 = [[UIImageView alloc]init];
    rightArrow2.image = [UIImage imageNamed:@"icon_right_arrow"];
    [_hiddenDangerView sd_addSubviews:@[_hiddenDangerTitle,_hiddenDangerValue,rightArrow2]];
    _hiddenDangerTitle.sd_layout.widthIs(122).heightIs(15).leftSpaceToView(_hiddenDangerView, 12)
    .topSpaceToView(_hiddenDangerView, 14.5);
    _hiddenDangerValue.sd_layout.widthIs(92).heightIs(15).rightSpaceToView(_hiddenDangerView, 28)
    .topSpaceToView(_hiddenDangerView, 14.5);
    rightArrow2.sd_layout.widthIs(6.9).heightIs(11.4).rightSpaceToView(_hiddenDangerView, 12)
    .topSpaceToView(_hiddenDangerView, 16.3);
    
    _localeView = [[UIView alloc]init];//是否现场处理
    _localeView.backgroundColor = [UIColor whiteColor];
    UITapGestureRecognizer *localeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickIsLocaleDispose)];
    [_localeView addGestureRecognizer:localeTap];
    _localeTitle= [[UILabel alloc]init];
    _localeTitle.text = @"是否现场处理";
    _localeTitle.textColor = ColorFromRGB(0x333333);
    _localeTitle.font = [UIFont systemFontOfSize:15.f];
    _localeValue= [[UILabel alloc]init];
    _localeValue.textColor = ColorFromRGB(0x999999);
    _localeValue.font = [UIFont systemFontOfSize:15.f];
    _localeValue.text = @"否";
    _localeValue.textAlignment = NSTextAlignmentRight;
    UIImageView *rightArrow3 = [[UIImageView alloc]init];
    rightArrow3.image = [UIImage imageNamed:@"icon_right_arrow"];
    [_localeView sd_addSubviews:@[_localeTitle,_localeValue,rightArrow3]];
    _localeTitle.sd_layout.widthIs(122).heightIs(15).leftSpaceToView(_localeView, 12)
    .topSpaceToView(_localeView, 14.5);
    _localeValue.sd_layout.widthIs(92).heightIs(15).rightSpaceToView(_localeView, 28)
    .topSpaceToView(_localeView, 14.5);
    rightArrow3.sd_layout.widthIs(6.9).heightIs(11.4).rightSpaceToView(_localeView, 12)
    .topSpaceToView(_localeView, 16.3);
    
    _disposeText = [[SZTextView alloc]init];//处理内容
    _disposeText.textColor = ColorFromRGB(0x666666);
//    _disposeText.description = @"请输入内容（140字以内）";
    _disposeText.placeholder =@"请输入内容（140字以内）";
    _disposeText.delegate = self;
    _disposeText.layer.cornerRadius = 6;
    _disposeText.layer.masksToBounds = YES;
    
    [self.contentView sd_addSubviews:@[_alarmTypeView,_hiddenDangerView,_localeView,_disposeText]];
    _alarmTypeView.sd_layout.heightIs(44).leftSpaceToView(self.contentView,8)
    .rightSpaceToView(self.contentView, 8).topSpaceToView(self.contentView, 8);
    
    _hiddenDangerView.sd_layout.heightIs(44).leftSpaceToView(self.contentView,8)
    .rightSpaceToView(self.contentView, 8).topSpaceToView(_alarmTypeView,1);
    
    _localeView.sd_layout.heightIs(44).leftSpaceToView(self.contentView,8)
    .rightSpaceToView(self.contentView, 8).topSpaceToView(_hiddenDangerView,1);
    
    _disposeText.sd_layout.heightIs(100).leftSpaceToView(self.contentView,8)
    .rightSpaceToView(self.contentView, 8).topSpaceToView(_localeView,8);
    
    self.contentView.backgroundColor =  ColorFromRGB(0xeff0f4);
    
    [self setupAutoHeightWithBottomView:_disposeText bottomMargin:8];
    
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    NSString *toBeString = textView.text;
    NSInteger maxLength = 140;
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position || !selectedRange) {
        if (toBeString.length > maxLength) {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1) {
                textView.text = [toBeString substringToIndex:maxLength];
               
                
            } else {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    if(self.getDisposeTextBlock){
        self.getDisposeTextBlock(textView.text);
    }
    
}

-(void)onClickAlarmType{
    if(self.alarmTypeBlock){
        self.alarmTypeBlock();
    }
}

-(void)onClickHiddenDanger{
    if(self.hiddenDangerBlock){
        self.hiddenDangerBlock();
    }
}

-(void)onClickIsLocaleDispose{
    if(self.localeDisposeBlock){
        self.localeDisposeBlock();
    }
}


-(void)setTypeDatas:(NSMutableArray<NSNumber*> *)types contentText:(NSString *)text{
    if(types){
        NSInteger alatm = [types[0] integerValue];
        switch (alatm) {
            case 0:
                _alarmTypeValue.text = @"请选择";
                break;
            case 1:
                _alarmTypeValue.text = @"火警";
                break;
            case 2:
                 _alarmTypeValue.text = @"预警";
                break;
            case 3:
                 _alarmTypeValue.text = @"故障";
                break;
            default:
                break;
        }
        if([types[1] integerValue] == 0){
            _hiddenDangerValue.text = @"否";
        }else if([types[1] integerValue] == 1){
            _hiddenDangerValue.text = @"是";
        }
        
        if([types[2] integerValue] == 0){
            _localeValue.text = @"否";
        }else if([types[2] integerValue] == 1){
            _localeValue.text = @"是";
        }
        _disposeText.text = text;
        
     
    }
}

@end
